package com.bjypc.youth.web.json;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.taglibs.standard.tag.common.core.OutSupport;

import com.bjypc.youth.entity.ResearchArticle;
import com.bjypc.youth.service.IResearchArticleService;
import com.opensymphony.xwork2.Action;

public class DownloadResearchArticleAction implements Action {
	private Logger logger = Logger
			.getLogger(DownloadResearchArticleAction.class);
	private String fileName;// 初始的通过param指定的文件名属性
	private String inputPath;// 指定要被下载的文件路径
	private Long id;
	private InputStream inputStream;
	private IResearchArticleService researchArticleService;

	public DownloadResearchArticleAction() {
		super();
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public InputStream getInputStream() throws Exception {
		// 通过 ServletContext，也就是application 来读取数据
		return ServletActionContext.getServletContext().getResourceAsStream(
				inputPath);
	}

	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		ResearchArticle article = this.researchArticleService.find(id);
		String articleName = article.getTitle() + article.getSuffix();
		fileName = getDownloadFileName(articleName);
		OutputStream out = response.getOutputStream();
		response.setHeader("Content-disposition", "attachment; filename="
				+ fileName);
		response.setContentType("application/msexcel;charset=UTF-8");

		String downloadPath = ServletActionContext.getServletContext()
				.getRealPath("/download/")
				+ File.separator + id + article.getSuffix();
		logger.info("downloadPath:" + downloadPath + ",fileName:" + fileName);
		File file = new File(downloadPath);
		byte[] contents=new byte[8092];
		if(!file.exists()){
			InputStream  input=article.getContent().getBinaryStream();
			contents=new byte[input.available()];
			input.read(contents);
			FileUtils.writeByteArrayToFile(file, contents);
		}else{
			contents= FileUtils.readFileToByteArray(file);
		}
		 
		out.write(contents);
		out.flush();
		out.close();
		return SUCCESS;
	}

	public void setInputPath(String value) {
		inputPath = value;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	/** 提供转换编码后的供下载用的文件名 */

	public String getDownloadFileName() {
		String downFileName = fileName;
		try {
			downFileName = new String(downFileName.getBytes(), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return downFileName;
	}

	public String getDownloadFileName(String name) {
		String downFileName = name;
		try {
			downFileName = new String(downFileName.getBytes(), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return downFileName;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public IResearchArticleService getResearchArticleService() {
		return researchArticleService;
	}

	public void setResearchArticleService(
			IResearchArticleService researchArticleService) {
		this.researchArticleService = researchArticleService;
	}

}
